package com.ebupt.migu.music.api.service.impl;

import com.ebupt.migu.music.api.entity.SendMessageEntity;
import com.ebupt.migu.music.api.mapper.ServiceErrorReportMapper;
import com.ebupt.migu.music.api.service.ServiceErrorReportService;
import com.ebupt.migu.music.common.sms.Sender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @description:
 * @author: liuyangyang@cd.ebupt.com
 * @create: 2020-11-23 09:57
 */
@Slf4j
@Service
public class ServiceErrorReportServiceImpl implements ServiceErrorReportService {

    @Autowired
    private ServiceErrorReportMapper mapper;

    /**
     * 模板ID
     */
    @Value("${soam.template_id}")
    private String SOAM_TEMPLATE_ID;

    /**
     * 后台运维收信人
     */
    @Value("${soam.maintainer}")
    private String SOAM_MAINTAINER;

    /**
     * 发送短信
     */
    @Override
    public void send_message(SendMessageEntity entity) {
        Map<String, Object> map = new HashMap<>();
        // 策略或人群的ID、名称
        String target_id;
        String target_name;
        String error_time;
        String code = entity.getError_code();
        if (null != entity.getGroup_id() && !"".equals(entity.getGroup_id())) {
            target_id = entity.getGroup_id();
            map = mapper.user_group_info(target_id);
            if ( map == null){
                return;
            }
            target_name = String.valueOf(map.get("userGroupName"));
            error_time = String.valueOf(map.get("modifyTime"));

        } else {
            target_id = entity.getTac_id();
            map = mapper.tac_info(target_id);
            target_name = String.valueOf(map.get("strategyName"));
            error_time = String.valueOf(map.get("executeTime"));
        }
        // 创建时间
        String create_time = String.valueOf(map.get("createTime"));
        // 创建人
        String creator = String.valueOf(map.get("creatorName"));
        // 所属部门
        String department = String.valueOf(map.get("creatorDepartment"));
        // 电话号码
        String phone = String.valueOf(map.get("creatorPhone"));
        // 短信模板参数
        String[] vars;
        // 收件人电话号码
        String[] tels;
        // 收件人： 1- 创建人；2- 维护人；
        if ("1".equals(entity.getAddressee())) {
            vars = message_content_to_creator(creator, create_time, target_id, target_name, error_time, code);
            tels = phone.split(",");
        } else {
            vars = message_content_to_maintainer(department, creator, create_time, target_id, target_name, error_time, code);
            tels = SOAM_MAINTAINER.split(",");
        }
        // 发送短信
        Sender.sendSMS(SOAM_TEMPLATE_ID, tels, vars);
    }

    /**
     * 创建人短信模板
     *
     * @param addressee
     * @param create_time
     * @param target_id
     * @param target_name
     * @param error_time
     * @param code
     * @return
     */
    private String[] message_content_to_creator(String addressee, String create_time, String target_id, String target_name, String error_time, String code) {
        List<String> vars = new ArrayList<>();
        vars.add(addressee);
        StringBuffer str = new StringBuffer();
        str.append("您于");
        str.append(create_time);
        str.append("在咪咕运营服务云平台创建的编号为");
        str.append(target_id);
        str.append("的");
        str.append(target_name);
        str.append(error_code(code));
        vars.add(String.valueOf(str));
        return list_to_array(vars);
    }

    /**
     * 维护人员短信模板
     *
     * @param department
     * @param creator
     * @param create_time
     * @param target_id
     * @param target_name
     * @param error_time
     * @param code
     * @return
     */
    private String[] message_content_to_maintainer(String department, String creator, String create_time, String target_id, String target_name, String error_time, String code) {
        List<String> vars = new ArrayList<>();
        vars.add("咪咕运营服务云系统维护人员");
        StringBuffer str = new StringBuffer();
        str.append(department);
        str.append(creator);
        str.append("于");
        str.append(create_time);
        str.append("在咪咕运营服务云平台创建的编号为");
        str.append(target_id);
        str.append("的");
        str.append(target_name);
        str.append(error_code(code));
        vars.add(String.valueOf(str));
        return list_to_array(vars);
    }

    /**
     * 转数组
     *
     * @param lists
     * @return
     */
    private String[] list_to_array(List<String> lists) {
        String[] array_temp = new String[lists.size()];
        String[] array = lists.toArray(array_temp);
        return array;
    }

    /**
     * 错误信息
     *
     * @param code
     * @return
     */
    private String error_code(String code) {
        Map<String, String> info = new HashMap<>();
        info.put("1", "执行时人群规模为0");
        info.put("2", "未按时执行");
        info.put("3", "已经生成");
        info.put("4", "生成失败");
        info.put("5", "生成超时");
        return info.get(code);
    }

}
